﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.MQTT
{
	/// <summary>
	/// Mqtt协议的客户端实现，支持订阅消息，发布消息，详细的使用例子参考api文档<br />
	/// The client implementation of the Mqtt protocol supports subscription messages and publishing messages. For detailed usage examples, refer to the api documentation. 
	/// </summary>
	/// <remarks>
	/// 这是一个MQTT的客户端实现，参照MQTT协议的3.1.1版本设计实现的。服务器可以是其他的组件提供的，其他的可以参考示例<br />
	/// This is an MQTT client implementation, designed and implemented with reference to version 3.1.1 of the MQTT protocol. The server can be provided by other components.
	/// </remarks>
	/// <example>
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test" title="简单的实例化" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test2" title="带用户名密码的实例化" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test3" title="连接示例" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test4" title="发布示例" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test5" title="订阅示例" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test8" title="网络重连示例" />
	/// </example>
	// Token: 0x020000BE RID: 190
	public class MqttClient : NetworkXBase
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		/// <param name="options">配置信息</param>
		// Token: 0x06000E18 RID: 3608 RVA: 0x0005340C File Offset: 0x0005160C
		public MqttClient(MqttConnectionOptions options)
		{
			this.connectionOptions = options;
			this.incrementCount = new SoftIncrementCount(65535L, 1L, 1);
			this.listLock = new object();
			this.publishMessages = new List<MqttPublishMessage>();
			this.subcribeTopics = new List<string>();
			this.activeTime = DateTime.Now;
			this.subcribeLock = new object();
		}

		/// <summary>
		/// 连接服务器，如果连接失败，请稍候重试。<br />
		/// Connect to the server. If the connection fails, try again later.
		/// </summary>
		/// <returns>连接是否成功</returns>
		// Token: 0x06000E19 RID: 3609 RVA: 0x00053484 File Offset: 0x00051684
		public OperateResult ConnectServer()
		{
			bool flag = this.connectionOptions == null;
			OperateResult result;
			if (flag)
			{
				result = new OperateResult("Optines is null");
			}
			else
			{
				Socket coreSocket = this.CoreSocket;
				if (coreSocket != null)
				{
					coreSocket.Close();
				}
				OperateResult<Socket> operateResult = base.CreateSocketAndConnect(this.connectionOptions.IpAddress, this.connectionOptions.Port, this.connectionOptions.ConnectTimeout);
				bool flag2 = !operateResult.IsSuccess;
				if (flag2)
				{
					result = operateResult;
				}
				else
				{
					this.CoreSocket = operateResult.Content;
					OperateResult<byte[]> operateResult2 = MqttHelper.BuildConnectMqttCommand(this.connectionOptions, "MQTT");
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = operateResult2;
					}
					else
					{
						OperateResult operateResult3 = base.Send(this.CoreSocket, operateResult2.Content);
						bool flag4 = !operateResult3.IsSuccess;
						if (flag4)
						{
							result = operateResult3;
						}
						else
						{
							OperateResult<byte, byte[]> operateResult4 = base.ReceiveMqttMessage(this.CoreSocket, 30000, null);
							bool flag5 = !operateResult4.IsSuccess;
							if (flag5)
							{
								result = operateResult4;
							}
							else
							{
								OperateResult operateResult5 = MqttHelper.CheckConnectBack(operateResult4.Content1, operateResult4.Content2);
								bool flag6 = !operateResult5.IsSuccess;
								if (flag6)
								{
									Socket coreSocket2 = this.CoreSocket;
									if (coreSocket2 != null)
									{
										coreSocket2.Close();
									}
									result = operateResult5;
								}
								else
								{
									this.incrementCount.ResetCurrentValue();
									this.closed = false;
									try
									{
										this.CoreSocket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(this.ReceiveAsyncCallback), this.CoreSocket);
									}
									catch (Exception ex)
									{
										return new OperateResult(ex.Message);
									}
									MqttClient.OnClientConnectedDelegate onClientConnected = this.OnClientConnected;
									if (onClientConnected != null)
									{
										onClientConnected(this);
									}
									Timer timer = this.timerCheck;
									if (timer != null)
									{
										timer.Dispose();
									}
									this.activeTime = DateTime.Now;
									bool flag7 = (int)this.connectionOptions.KeepAliveSendInterval.TotalMilliseconds > 0;
									if (flag7)
									{
										this.timerCheck = new Timer(new TimerCallback(this.TimerCheckServer), null, 2000, (int)this.connectionOptions.KeepAliveSendInterval.TotalMilliseconds);
									}
									result = OperateResult.CreateSuccessResult();
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 关闭Mqtt服务器的连接。<br />
		/// Close the connection to the Mqtt server.
		/// </summary>
		// Token: 0x06000E1A RID: 3610 RVA: 0x000536BC File Offset: 0x000518BC
		public void ConnectClose()
		{
			this.closed = true;
			OperateResult<byte[]> operateResult = MqttHelper.BuildMqttCommand(14, 0, null, null);
			bool isSuccess = operateResult.IsSuccess;
			if (isSuccess)
			{
				base.Send(this.CoreSocket, operateResult.Content);
			}
			Timer timer = this.timerCheck;
			if (timer != null)
			{
				timer.Dispose();
			}
			Thread.Sleep(20);
			Socket coreSocket = this.CoreSocket;
			if (coreSocket != null)
			{
				coreSocket.Close();
			}
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttClient.ConnectServer" />
		// Token: 0x06000E1B RID: 3611 RVA: 0x00053728 File Offset: 0x00051928
		[DebuggerStepThrough]
		public Task<OperateResult> ConnectServerAsync()
		{
			MqttClient.<ConnectServerAsync>d__3 <ConnectServerAsync>d__ = new MqttClient.<ConnectServerAsync>d__3();
			<ConnectServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<ConnectServerAsync>d__.<>4__this = this;
			<ConnectServerAsync>d__.<>1__state = -1;
			<ConnectServerAsync>d__.<>t__builder.Start<MqttClient.<ConnectServerAsync>d__3>(ref <ConnectServerAsync>d__);
			return <ConnectServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttClient.ConnectClose" />
		// Token: 0x06000E1C RID: 3612 RVA: 0x0005376C File Offset: 0x0005196C
		[DebuggerStepThrough]
		public Task ConnectCloseAsync()
		{
			MqttClient.<ConnectCloseAsync>d__4 <ConnectCloseAsync>d__ = new MqttClient.<ConnectCloseAsync>d__4();
			<ConnectCloseAsync>d__.<>t__builder = AsyncTaskMethodBuilder.Create();
			<ConnectCloseAsync>d__.<>4__this = this;
			<ConnectCloseAsync>d__.<>1__state = -1;
			<ConnectCloseAsync>d__.<>t__builder.Start<MqttClient.<ConnectCloseAsync>d__4>(ref <ConnectCloseAsync>d__);
			return <ConnectCloseAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 发布一个MQTT协议的消息到服务器。该消息包含主题，负载数据，消息等级，是否保留信息。<br />
		/// Publish an MQTT protocol message to the server. The message contains the subject, payload data, message level, and whether to retain information.
		/// </summary>
		/// <param name="message">消息</param>
		/// <returns>发布结果</returns>
		/// <example>
		/// 参照 <see cref="T:HslCommunication.MQTT.MqttClient" /> 的示例说明。
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test" title="简单的实例化" />
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test4" title="发布示例" />
		/// </example>
		// Token: 0x06000E1D RID: 3613 RVA: 0x000537B0 File Offset: 0x000519B0
		public OperateResult PublishMessage(MqttApplicationMessage message)
		{
			MqttPublishMessage mqttPublishMessage = new MqttPublishMessage
			{
				Identifier = ((message.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce) ? 0 : ((int)this.incrementCount.GetCurrentValue())),
				Message = message
			};
			OperateResult<byte[]> operateResult = MqttHelper.BuildPublishMqttCommand(mqttPublishMessage);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				bool flag2 = message.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce;
				if (flag2)
				{
					result = base.Send(this.CoreSocket, operateResult.Content);
				}
				else
				{
					this.AddPublishMessage(mqttPublishMessage);
					result = base.Send(this.CoreSocket, operateResult.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttClient.PublishMessage(HslCommunication.MQTT.MqttApplicationMessage)" />
		// Token: 0x06000E1E RID: 3614 RVA: 0x00053848 File Offset: 0x00051A48
		[DebuggerStepThrough]
		public Task<OperateResult> PublishMessageAsync(MqttApplicationMessage message)
		{
			MqttClient.<PublishMessageAsync>d__6 <PublishMessageAsync>d__ = new MqttClient.<PublishMessageAsync>d__6();
			<PublishMessageAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<PublishMessageAsync>d__.<>4__this = this;
			<PublishMessageAsync>d__.message = message;
			<PublishMessageAsync>d__.<>1__state = -1;
			<PublishMessageAsync>d__.<>t__builder.Start<MqttClient.<PublishMessageAsync>d__6>(ref <PublishMessageAsync>d__);
			return <PublishMessageAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 从服务器订阅一个或多个主题信息<br />
		/// Subscribe to one or more topics from the server
		/// </summary>
		/// <param name="topic">主题信息</param>
		/// <returns>订阅结果</returns>
		/// <example>
		/// 参照 <see cref="T:HslCommunication.MQTT.MqttClient" /> 的示例说明。
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test" title="简单的实例化" />
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test5" title="订阅示例" />
		/// </example>
		// Token: 0x06000E1F RID: 3615 RVA: 0x00053893 File Offset: 0x00051A93
		public OperateResult SubscribeMessage(string topic)
		{
			return this.SubscribeMessage(new string[]
			{
				topic
			});
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttClient.SubscribeMessage(System.String)" />
		// Token: 0x06000E20 RID: 3616 RVA: 0x000538A8 File Offset: 0x00051AA8
		public OperateResult SubscribeMessage(string[] topics)
		{
			MqttSubscribeMessage subcribeMessage = new MqttSubscribeMessage
			{
				Identifier = (int)this.incrementCount.GetCurrentValue(),
				Topics = topics
			};
			return this.SubscribeMessage(subcribeMessage);
		}

		/// <summary>
		/// 向服务器订阅一个主题消息，可以指定订阅的主题数组，订阅的质量等级，还有消息标识符<br />
		/// To subscribe to a topic message from the server, you can specify the subscribed topic array, 
		/// the subscription quality level, and the message identifier
		/// </summary>
		/// <param name="subcribeMessage">订阅的消息本体</param>
		/// <returns>是否订阅成功</returns>
		// Token: 0x06000E21 RID: 3617 RVA: 0x000538E4 File Offset: 0x00051AE4
		public OperateResult SubscribeMessage(MqttSubscribeMessage subcribeMessage)
		{
			bool flag = subcribeMessage.Topics == null;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateSuccessResult();
			}
			else
			{
				bool flag2 = subcribeMessage.Topics.Length == 0;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult();
				}
				else
				{
					OperateResult<byte[]> operateResult = MqttHelper.BuildSubscribeMqttCommand(subcribeMessage);
					bool flag3 = !operateResult.IsSuccess;
					if (flag3)
					{
						result = operateResult;
					}
					else
					{
						OperateResult operateResult2 = base.Send(this.CoreSocket, operateResult.Content);
						bool flag4 = !operateResult2.IsSuccess;
						if (flag4)
						{
							result = operateResult2;
						}
						else
						{
							this.AddSubTopics(subcribeMessage.Topics);
							result = OperateResult.CreateSuccessResult();
						}
					}
				}
			}
			return result;
		}

		// Token: 0x06000E22 RID: 3618 RVA: 0x00053978 File Offset: 0x00051B78
		private void AddSubTopics(string[] topics)
		{
			object obj = this.subcribeLock;
			lock (obj)
			{
				for (int i = 0; i < topics.Length; i++)
				{
					bool flag2 = !this.subcribeTopics.Contains(topics[i]);
					if (flag2)
					{
						this.subcribeTopics.Add(topics[i]);
					}
				}
			}
		}

		/// <summary>
		/// 取消订阅多个主题信息，取消之后，当前的订阅数据就不在接收到，除非服务器强制推送。<br />
		/// Unsubscribe from multiple topic information. After cancellation, the current subscription data will not be received unless the server forces it to push it.
		/// </summary>
		/// <param name="topics">主题信息</param>
		/// <returns>取消订阅结果</returns>
		/// <example>
		/// 参照 <see cref="T:HslCommunication.MQTT.MqttClient" /> 的示例说明。
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test" title="简单的实例化" />
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test7" title="订阅示例" />
		/// </example>
		// Token: 0x06000E23 RID: 3619 RVA: 0x000539F4 File Offset: 0x00051BF4
		public OperateResult UnSubscribeMessage(string[] topics)
		{
			MqttSubscribeMessage message = new MqttSubscribeMessage
			{
				Identifier = (int)this.incrementCount.GetCurrentValue(),
				Topics = topics
			};
			OperateResult<byte[]> operateResult = MqttHelper.BuildUnSubscribeMqttCommand(message);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = base.Send(this.CoreSocket, operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					this.RemoveSubTopics(topics);
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <summary>
		/// 取消订阅置顶的主题信息
		/// </summary>
		/// <param name="topic">主题信息</param>
		/// <returns>取消订阅结果</returns>
		/// <example>
		/// 参照 <see cref="T:HslCommunication.MQTT.MqttClient" /> 的示例说明。
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test" title="简单的实例化" />
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\MQTT\MQTTClient.cs" region="Test7" title="订阅示例" />
		/// </example>
		// Token: 0x06000E24 RID: 3620 RVA: 0x00053A76 File Offset: 0x00051C76
		public OperateResult UnSubscribeMessage(string topic)
		{
			return this.UnSubscribeMessage(new string[]
			{
				topic
			});
		}

		// Token: 0x06000E25 RID: 3621 RVA: 0x00053A88 File Offset: 0x00051C88
		private void RemoveSubTopics(string[] topics)
		{
			object obj = this.subcribeLock;
			lock (obj)
			{
				for (int i = 0; i < topics.Length; i++)
				{
					bool flag2 = this.subcribeTopics.Contains(topics[i]);
					if (flag2)
					{
						this.subcribeTopics.Remove(topics[i]);
					}
				}
			}
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttClient.SubscribeMessage(System.String)" />
		// Token: 0x06000E26 RID: 3622 RVA: 0x00053B00 File Offset: 0x00051D00
		[DebuggerStepThrough]
		public Task<OperateResult> SubscribeMessageAsync(string topic)
		{
			MqttClient.<SubscribeMessageAsync>d__14 <SubscribeMessageAsync>d__ = new MqttClient.<SubscribeMessageAsync>d__14();
			<SubscribeMessageAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<SubscribeMessageAsync>d__.<>4__this = this;
			<SubscribeMessageAsync>d__.topic = topic;
			<SubscribeMessageAsync>d__.<>1__state = -1;
			<SubscribeMessageAsync>d__.<>t__builder.Start<MqttClient.<SubscribeMessageAsync>d__14>(ref <SubscribeMessageAsync>d__);
			return <SubscribeMessageAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttClient.SubscribeMessage(System.String[])" />
		// Token: 0x06000E27 RID: 3623 RVA: 0x00053B4C File Offset: 0x00051D4C
		[DebuggerStepThrough]
		public Task<OperateResult> SubscribeMessageAsync(string[] topics)
		{
			MqttClient.<SubscribeMessageAsync>d__15 <SubscribeMessageAsync>d__ = new MqttClient.<SubscribeMessageAsync>d__15();
			<SubscribeMessageAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<SubscribeMessageAsync>d__.<>4__this = this;
			<SubscribeMessageAsync>d__.topics = topics;
			<SubscribeMessageAsync>d__.<>1__state = -1;
			<SubscribeMessageAsync>d__.<>t__builder.Start<MqttClient.<SubscribeMessageAsync>d__15>(ref <SubscribeMessageAsync>d__);
			return <SubscribeMessageAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttClient.UnSubscribeMessage(System.String[])" />
		// Token: 0x06000E28 RID: 3624 RVA: 0x00053B98 File Offset: 0x00051D98
		[DebuggerStepThrough]
		public Task<OperateResult> UnSubscribeMessageAsync(string[] topics)
		{
			MqttClient.<UnSubscribeMessageAsync>d__16 <UnSubscribeMessageAsync>d__ = new MqttClient.<UnSubscribeMessageAsync>d__16();
			<UnSubscribeMessageAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<UnSubscribeMessageAsync>d__.<>4__this = this;
			<UnSubscribeMessageAsync>d__.topics = topics;
			<UnSubscribeMessageAsync>d__.<>1__state = -1;
			<UnSubscribeMessageAsync>d__.<>t__builder.Start<MqttClient.<UnSubscribeMessageAsync>d__16>(ref <UnSubscribeMessageAsync>d__);
			return <UnSubscribeMessageAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttClient.UnSubscribeMessage(System.String)" />
		// Token: 0x06000E29 RID: 3625 RVA: 0x00053BE4 File Offset: 0x00051DE4
		[DebuggerStepThrough]
		public Task<OperateResult> UnSubscribeMessageAsync(string topic)
		{
			MqttClient.<UnSubscribeMessageAsync>d__17 <UnSubscribeMessageAsync>d__ = new MqttClient.<UnSubscribeMessageAsync>d__17();
			<UnSubscribeMessageAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<UnSubscribeMessageAsync>d__.<>4__this = this;
			<UnSubscribeMessageAsync>d__.topic = topic;
			<UnSubscribeMessageAsync>d__.<>1__state = -1;
			<UnSubscribeMessageAsync>d__.<>t__builder.Start<MqttClient.<UnSubscribeMessageAsync>d__17>(ref <UnSubscribeMessageAsync>d__);
			return <UnSubscribeMessageAsync>d__.<>t__builder.Task;
		}

		// Token: 0x06000E2A RID: 3626 RVA: 0x00053C30 File Offset: 0x00051E30
		private void OnMqttNetworkError()
		{
			bool flag = this.closed;
			if (!flag)
			{
				bool flag2 = Interlocked.CompareExchange(ref this.isReConnectServer, 1, 0) == 0;
				if (flag2)
				{
					try
					{
						bool flag3 = this.OnNetworkError == null;
						if (flag3)
						{
							ILogNet logNet = base.LogNet;
							if (logNet != null)
							{
								logNet.WriteInfo(this.ToString(), "The network is abnormal, and the system is ready to automatically reconnect after 10 seconds.");
							}
							for (;;)
							{
								for (int i = 0; i < 10; i++)
								{
									Thread.Sleep(1000);
									ILogNet logNet2 = base.LogNet;
									if (logNet2 != null)
									{
										logNet2.WriteInfo(this.ToString(), string.Format("Wait for {0} second to connect to the server ...", 10 - i));
									}
								}
								OperateResult operateResult = this.ConnectServer();
								bool isSuccess = operateResult.IsSuccess;
								if (isSuccess)
								{
									break;
								}
								ILogNet logNet3 = base.LogNet;
								if (logNet3 != null)
								{
									logNet3.WriteInfo(this.ToString(), "The connection failed. Prepare to reconnect after 10 seconds.");
								}
							}
							ILogNet logNet4 = base.LogNet;
							if (logNet4 != null)
							{
								logNet4.WriteInfo(this.ToString(), "Successfully connected to the server!");
							}
						}
						else
						{
							EventHandler onNetworkError = this.OnNetworkError;
							if (onNetworkError != null)
							{
								onNetworkError(this, new EventArgs());
							}
						}
						Interlocked.Exchange(ref this.isReConnectServer, 0);
					}
					catch
					{
						Interlocked.Exchange(ref this.isReConnectServer, 0);
						throw;
					}
				}
			}
		}

		// Token: 0x06000E2B RID: 3627 RVA: 0x00053DA0 File Offset: 0x00051FA0
		[DebuggerStepThrough]
		private void ReceiveAsyncCallback(IAsyncResult ar)
		{
			MqttClient.<ReceiveAsyncCallback>d__19 <ReceiveAsyncCallback>d__ = new MqttClient.<ReceiveAsyncCallback>d__19();
			<ReceiveAsyncCallback>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<ReceiveAsyncCallback>d__.<>4__this = this;
			<ReceiveAsyncCallback>d__.ar = ar;
			<ReceiveAsyncCallback>d__.<>1__state = -1;
			<ReceiveAsyncCallback>d__.<>t__builder.Start<MqttClient.<ReceiveAsyncCallback>d__19>(ref <ReceiveAsyncCallback>d__);
		}

		// Token: 0x06000E2C RID: 3628 RVA: 0x00053DE0 File Offset: 0x00051FE0
		private void ExtraPublishData(byte mqttCode, byte[] data)
		{
			this.activeTime = DateTime.Now;
			OperateResult<string, byte[]> operateResult = MqttHelper.ExtraMqttReceiveData(mqttCode, data);
			bool flag = !operateResult.IsSuccess;
			if (flag)
			{
				ILogNet logNet = base.LogNet;
				if (logNet != null)
				{
					logNet.WriteDebug(this.ToString(), operateResult.Message);
				}
			}
			else
			{
				MqttClient.MqttMessageReceiveDelegate onMqttMessageReceived = this.OnMqttMessageReceived;
				if (onMqttMessageReceived != null)
				{
					onMqttMessageReceived(operateResult.Content1, operateResult.Content2);
				}
			}
		}

		// Token: 0x06000E2D RID: 3629 RVA: 0x00053E50 File Offset: 0x00052050
		private void TimerCheckServer(object obj)
		{
			bool flag = this.CoreSocket != null;
			if (flag)
			{
				bool flag2 = (DateTime.Now - this.activeTime).TotalSeconds > this.connectionOptions.KeepAliveSendInterval.TotalSeconds * 3.0;
				if (flag2)
				{
					this.OnMqttNetworkError();
				}
				else
				{
					bool flag3 = !base.Send(this.CoreSocket, MqttHelper.BuildMqttCommand(12, 0, new byte[0], new byte[0]).Content).IsSuccess;
					if (flag3)
					{
						this.OnMqttNetworkError();
					}
				}
			}
		}

		// Token: 0x06000E2E RID: 3630 RVA: 0x00009AA1 File Offset: 0x00007CA1
		private void AddPublishMessage(MqttPublishMessage publishMessage)
		{
		}

		/// <summary>
		/// 当接收到Mqtt订阅的信息的时候触发
		/// </summary>
		// Token: 0x14000012 RID: 18
		// (add) Token: 0x06000E2F RID: 3631 RVA: 0x00053EF0 File Offset: 0x000520F0
		// (remove) Token: 0x06000E30 RID: 3632 RVA: 0x00053F28 File Offset: 0x00052128
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event MqttClient.MqttMessageReceiveDelegate OnMqttMessageReceived;

		/// <summary>
		/// 当网络发生异常的时候触发的事件，用户应该在事件里进行重连服务器
		/// </summary>
		// Token: 0x14000013 RID: 19
		// (add) Token: 0x06000E31 RID: 3633 RVA: 0x00053F60 File Offset: 0x00052160
		// (remove) Token: 0x06000E32 RID: 3634 RVA: 0x00053F98 File Offset: 0x00052198
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event EventHandler OnNetworkError;

		/// <summary>
		/// 当客户端连接成功触发事件，就算是重新连接服务器后，也是会触发的<br />
		/// The event is triggered when the client is connected successfully, even after reconnecting to the server.
		/// </summary>
		// Token: 0x14000014 RID: 20
		// (add) Token: 0x06000E33 RID: 3635 RVA: 0x00053FD0 File Offset: 0x000521D0
		// (remove) Token: 0x06000E34 RID: 3636 RVA: 0x00054008 File Offset: 0x00052208
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event MqttClient.OnClientConnectedDelegate OnClientConnected;

		/// <inheritdoc />
		// Token: 0x06000E35 RID: 3637 RVA: 0x0005403D File Offset: 0x0005223D
		public override string ToString()
		{
			return string.Format("MqttClient[{0}:{1}]", this.connectionOptions.IpAddress, this.connectionOptions.Port);
		}

		// Token: 0x040003BA RID: 954
		private DateTime activeTime;

		// Token: 0x040003BB RID: 955
		private int isReConnectServer = 0;

		// Token: 0x040003BC RID: 956
		private List<MqttPublishMessage> publishMessages;

		// Token: 0x040003BD RID: 957
		private object listLock;

		// Token: 0x040003BE RID: 958
		private List<string> subcribeTopics;

		// Token: 0x040003BF RID: 959
		private object subcribeLock;

		// Token: 0x040003C0 RID: 960
		private SoftIncrementCount incrementCount;

		// Token: 0x040003C1 RID: 961
		private bool closed = false;

		// Token: 0x040003C2 RID: 962
		private MqttConnectionOptions connectionOptions;

		// Token: 0x040003C3 RID: 963
		private Timer timerCheck;

		/// <summary>
		/// 当接收到Mqtt订阅的信息的时候触发<br />
		/// Triggered when receiving Mqtt subscription information
		/// </summary>
		/// <param name="topic">主题信息</param>
		/// <param name="payload">负载数据</param>
		// Token: 0x0200043B RID: 1083
		// (Invoke) Token: 0x06002936 RID: 10550
		public delegate void MqttMessageReceiveDelegate(string topic, byte[] payload);

		/// <summary>
		/// 连接服务器成功的委托<br />
		/// Connection server successfully delegated
		/// </summary>
		// Token: 0x0200043C RID: 1084
		// (Invoke) Token: 0x0600293A RID: 10554
		public delegate void OnClientConnectedDelegate(MqttClient client);
	}
}
